package com.pro.controller;

import com.pro.pojo.JsUserCoupon;
import com.pro.pojo.JsUserCouponUsed;
import com.pro.pojo.JsonData;
import com.pro.service.JsUserCouponService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * ClassName:JsUserCouponController
 * Package:com.pro.controller
 * Description: 优惠券controller层
 *
 * @Date:2022/1/10 下午 3:41
 * @Author: WYH
 */
@RestController
@RequestMapping("/coupon")
public class JsUserCouponController {

    @Autowired
    private JsUserCouponService jsUserCouponService;

    @Autowired
    private FastDFSClientUtil dfsClient;

    /**
     * @author WYH
     * @date 2022/1/10 下午 3:02
     * @content : 查询优惠券信息，包括筛选查询以及高级检索
     */
    @RequestMapping("/getAllCoupon")
    public JsonData getAllCoupon(String couponName, String online, String grant, String startDate, String endDate, Integer limit, Integer page) {
        couponName = !("").equals(couponName) && couponName !=null?"%"+couponName+"%":"%%";
        online = !("").equals(online) && online !=null?"%"+online+"%":"%%";
        grant = !("").equals(grant) && grant !=null?"%"+grant+"%":"%%";
        if(startDate!=null && startDate!=""){
            startDate ="%"+startDate+"%";
        }else{
            startDate = "%%";
        }
        if(endDate != null && endDate !=""){
            endDate = "%"+endDate+"%";
        }else{
            endDate = "%%";
        }
        List<JsUserCoupon> allCoupon = jsUserCouponService.getAllCoupon(couponName,online,grant,startDate,endDate,limit, page);
        Integer count = jsUserCouponService.count();
        return allCoupon != null ? JsonData.buildSuccess(allCoupon,count): JsonData.buildError("数据格式异常");
    }

    /**
     * @author WYH
     * @date 2022/1/11 下午 3:41
     * @content : 修改优惠券状态 上线/不上线
     */
    @RequestMapping("/updateCoupon")
    public JsonData updateCoupon(Integer couponId, Integer online, Integer updateId, Date updateDate) {
        Boolean aBoolean = jsUserCouponService.updateCoupon(couponId, online, updateId, updateDate);
        return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("数据格式异常");
    }

    /**
     * @author WYH
     * @date 2022/1/11 下午 3:42
     * @content : 删除优惠券
     */
    @RequestMapping("/delCoupon")
    public JsonData delCoupon(Integer couponId){
        Boolean aBoolean = jsUserCouponService.delCoupon(couponId);
        return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("数据格式异常");
    }

    /**
     * @author WYH
     * @date 2022/1/11 下午 4:16
     * @content : 查询优惠券详情
     */
    @RequestMapping("/seeCoupon")
    public JsonData seeCoupon(Integer couponId){
        JsUserCoupon jsUserCoupon = jsUserCouponService.seeCoupon(couponId);
        return jsUserCoupon != null ? JsonData.buildSuccess(jsUserCoupon) : JsonData.buildError("数据格式异常");
    }
    
    /**
     * @Author WYH
     * @Description //TODO 添加优惠券
     * @Date 下午 8:20 2022/1/12
     * @Param jsUserCoupon
     * @return  true | false
     **/
    @RequestMapping("/addCoupon")
    public JsonData addCoupon(JsUserCoupon jsUserCoupon){
        Boolean addCouponBoolean = jsUserCouponService.addCoupon(jsUserCoupon);
        return addCouponBoolean != false ? JsonData.buildSuccess(addCouponBoolean) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 上传优惠券图片
     * @Date 下午 9:17 2022/1/12
     * @Param file，request
     * @return Map
     **/
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file) {
        String resAccessUrl =null;
        String fileUrl = null;
        try {
            fileUrl = dfsClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //路径
        resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }

    /**
     * @Author WYH
     * @Description //TODO 查询用户优惠券
     * @Date 上午 9:49 2022/1/13
     * @Param limit,page
     * @return  List<JsUserCoupon>
     **/
    @RequestMapping("/getAllUserCoupon")
    public JsonData getAllUserCoupon(String account,String nickname,Integer limit, Integer page){
        account = !("").equals(account) && account !=null?"%"+account+"%":"%%";
        nickname = !("").equals(nickname) && nickname !=null?"%"+nickname+"%":"%%";
        List<JsUserCoupon> allUserCoupon = jsUserCouponService.getAllUserCoupon(account,nickname,limit, page);
        Integer integer = jsUserCouponService.userCount(account,nickname);
        return allUserCoupon != null ? JsonData.buildSuccess(allUserCoupon,integer) : JsonData.buildError("数据结构异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 赠送优惠券中的查询优惠券
     * @Date 下午 7:36 2022/1/13
     * @Param couponName,limit,page
     * @return   List<JsUserCoupon>
     **/
    @RequestMapping("/getCouponGive")
    public JsonData getCouponGive(String couponName, Integer limit, Integer page){
        couponName = !("").equals(couponName) && couponName !=null?"%"+couponName+"%":"%%";
        List<JsUserCoupon> couponGive = jsUserCouponService.getCouponGive(couponName, limit, page);
        Integer giveCount = jsUserCouponService.GiveCount(couponName);
        return couponGive != null? JsonData.buildSuccess(couponGive,giveCount) : JsonData.buildError("数据格式异常") ;
    }

    /**
     * @Author WYH
     * @Description //TODO 为用户添加一个或多个优惠券
     * @Date 下午 8:31 2022/1/13
     * @Param List<JsUserCouponUsed>
     * @return  true | false
     **/
    @RequestMapping("/addGiveCoupon")
    public JsonData addGiveCoupon(JsUserCouponUsed jsUserCoupon){
        List<JsUserCouponUsed> list = new ArrayList<>();
        list.add(jsUserCoupon);
        Boolean addGiveCoupon = jsUserCouponService.addGiveCoupon(list);
        return addGiveCoupon != null ? JsonData.buildSuccess(addGiveCoupon) : JsonData.buildSuccess("数据格式异常");
    }
}
